<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>
      img {
        width: 300px;
      }
      .active {
        background-color: purple;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div @mouseover="stop" @mouseout="play">
        <img :src="imgs[i]" alt="" />
        <br />
        <button
          v-for="x in imgs.length"
          :class="{active:x-1==i}"
          @click="i=x-1"
        >
          {{x}}
        </button>
        <br />
        <button @click="i=i==0?imgs.length-1:i-1">上一页</button>
        <button @click="i=i==imgs.length-1?0:i+1">下一页</button>
      </div>
    </div>
  </body>
  <script>
    let app = Vue.createApp({
      data() {
        return {
          imgs: [
            "http://bfs.biyao.com/group2/M00/25/41/CghiFmUjncKAWi9iABjB-wFJJss625_800x800.jpg",
            "http://bfs.biyao.com/group2/M00/74/48/CghiFmL-5teACsFIAB8d5X_83SI816_800x800.jpg",
            "http://bfs.biyao.com/group2/M00/74/29/CghkFmL-5tqADMn7AB81pETgGgg629_800x800.jpg",
            "http://bfs.biyao.com/group2/M00/74/48/CghiFmL-5t2AXXncABit7bGMJQw778_800x800.jpg",
            "http://bfs.biyao.com/group2/M00/74/48/CghiFmL-5uCAZJ2eAByUosANuLQ523_800x800.jpg",
          ],
          i: 0,
        };
      },
      methods: {
        play() {
          // 方法内部的this指向当前实例 可以通过this存储公共访问的值
          this.timer = setInterval(() => {
            this.i = this.i == this.imgs.length - 1 ? 0 : this.i + 1;
          }, 2000);
        },
        stop() {
          clearInterval(this.timer);
        },
      },
    });
    app = app.mount("#app");
    app.play(); //初始调用
  </script>
</html>
